package Server;

import java.io.ObjectOutputStream;
import java.util.LinkedList;

import Command.BlokhaakDecoration;
import Command.HistoryTracker;
import Command.HomerDecoration;

public class commandList implements Runnable{
	private LinkedList<Command> commands = new LinkedList<Command>();
	private ObjectOutputStream output;

	public commandList(ObjectOutputStream oos) {
		output = oos;
		new Thread(this).start();
	}

	public void addCommand(Command command){
		commands.add(command);		
	}

	public void run(){

		HistoryTracker ht = HistoryTracker.getInstance();

		while(true){
			try {
				Thread.sleep(50); //save some cpu cycles
			} catch (Exception e) {

			}
			if(!commands.isEmpty()){
				try {
					synchronized (commands) {
						Command command = (Command) commands.poll();
						
						//decorators
						command = new BlokhaakDecoration(command);
						command = new HomerDecoration(command);
						
						
						ht.addCommand(command);
						output.writeObject(command.execute());
						output.flush();
					}
				} catch (Exception e) {
					e.printStackTrace();	
				}
			}
		}

	}	
}